
************************************************************************;
** Output Contract Level Frequent Used Procedures and Selected Rates  **;
** For 2005 HEDIS HMO dataset                                         **;
************************************************************************;

options ls=100 nofmterr ;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata' ;
libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';


data a0; set new.hedis2005;

     **--  Sample Selection --** ;
 
     if denom_flag=1;

     if state_code=' ' or state_code='00' or state_code>"53" or state_code in ("40","48") then delete;

     age = age+1;  /** Set Age to End of the Year **/

     if age>=65;

     length plantypec $ 3;

     if plan_type='HMO/HMOPOS' then plantypec='HMO' ;

     if plantypec='HMO';

     if age>65 and totmonth<12 then delete;

     death_dt=bene_death_dt;
     if (bene_valid_death_dt_sw='V' or death_dt>0) then death1=1; else death1=0;

     deathyear=year(death_dt);
     deathmon =month(death_dt);

     if death1=1 and
       (deathyear=2005 or
       (deathyear=2006 and (deathmon>0 and deathmon<3)))
     then delete ;

      if sub_id='6626' then org_id='HS524';
      if sub_id='6628' then org_id='HN524';

     **-- Recode 9 or 999 to 0s --**;

     array rate bcs_d bcs_n cmc_d cmc_n1 cdc_d1 cdc_n1 cdc_d3 cdc_n3 cdc_d4 cdc_n4 gso_d gso_n;  
     do over rate; 
        if rate=9 then rate=0; 
     end;

     array fps fspcabgn fspptcan fsp_cc_n fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn
               fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n fsp_masn fsp_lumn;
     do over fps; 
        if fps=999 then fps=0;  
     end;

     array uti acd_n_t  acd_s_n  acd_m_n  
               acdy_n_t acdy_s_n acdy_m_n  
               ac_ov_n  ac_erv_n ac_asp_n ac_ors_n ;
     do over uti;
        if uti=999 then uti=0;
     end;


     **-- Clean up Gender Related Miscoding - very low occurance --**;

     if sex=1 or age>69 then do; bcs_d=.;  bcs_n=.;   end;  /** female only**/
     if sex=1 then do; fsp_ah_n=.; fsp_vh_n=.;  fsp_masn=.; fsp_lumn=.; end;  /** female only**/
     if sex=2 then fsp_p_n=.;   /** male only**/
     if age>75 then do; cdc_d1=.; cdc_n1=.; cdc_d3=.; cdc_n3=.; cdc_d4=.; cdc_n4=.; end;


     **-- CAP measures  John and Bruce email 6-4-2010 --**;

     if fspcabgn>3 then fspcabgn=3;   
     if fspptcan>4 then fspptcan=4;   
     if fsp_cc_n>4 then fsp_cc_n=4;   
     
     array fps2 fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n fsp_masn fsp_lumn;
     do over fps2; 
          if fps2>2 then fps2=2;  
     end;
       
     if acd_n_t > 12 then acd_n_t = 12;   * Total discharges set max 1 per month * ;    
     if acd_s_n > 12 then acd_s_n = 12;   * Surgery discharges *;
     if acd_m_n > 12 then acd_m_n = 12;   * Medical discharges *;

     if acdy_n_t > 365 then acdy_n_t = 365;  * Maximum possible in 12 months *;
     if acdy_s_n > 365 then acdy_s_n = 365;
     if acdy_m_n > 365 then acdy_m_n = 365;

     if ac_ov_n  > 50 then ac_ov_n  = 50; * Tot Out Patient visit set max 1 per week *;
     if ac_erv_n > 12 then ac_erv_n = 12; * Set max 1 per month *;
     if ac_asp_n > 12 then ac_asp_n = 12; 
     if ac_ors_n > 12 then ac_ors_n = 12;

 
     %let rate  = bcs_d bcs_n cmc_d cmc_n1 cdc_d1 cdc_n1 cdc_d3 cdc_n3 cdc_d4 cdc_n4 gso_d gso_n ;
     %let fsp   = fspcabgn fspptcan fsp_cc_n fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn
                  fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n fsp_masn fsp_lumn;
     %let disch = acd_n_t  acd_s_n  acd_m_n ;
     %let days  = acdy_n_t acdy_s_n acdy_m_n ;
     %let visits= ac_ov_n  ac_erv_n ac_asp_n ac_ors_n ;

     cons = 1;

     proc freq data=a0 noprint;
          tables org_id / out=org_cnt;

     **-- Create Primary State for each Health Plan --**;

     data s0; set a0;

          proc freq noprint;
               tables org_id/out=c0;

        data c01(drop=count); set c0;

             tot_n = count;
             keep tot_n count org_id;
             proc sort; by org_id;

     data s1; set a0;

          length org_state $ 8;
          org_state = org_id||state_code;
          proc freq noprint;
               tables org_state/out=c1;

        data c11(drop=count); set c1;

             length org_id $ 5;
             org_id = substr(org_state,1,5);
             state_n = count;
             keep state_n count org_id org_state;
             proc sort; by org_id;

    data ss0; merge c01 c11; by org_id;

            pct_st = state_n / tot_n;
            proc sort; by descending pct_st;
            proc sort nodupkey; by org_id;    /** keep the entry with max state concentration **/

    data ss(drop=org_state); set ss0;

           cntr_state=substr(org_state,6,2);
           if pct_st>=0.5 then onest50pct=1; else onest50pct=0;
           keep org_id cntr_state pct_st onest50pct tot_n org_state;
           proc sort; by org_id;

  **-- Create a health plan info dataset --**;

  data org0; set a0;

       keep org_id org_name plantypec sub_id;
       proc sort nodupkey; by org_id;

  data org; merge org0(in=ok) ss; by org_id;

       if ok;

       *** Create Contract Region using primary contract state ***;

    if cntr_state='01' then state_ab='AL' ;    if cntr_state='02' then state_ab='AK' ;
    if cntr_state='03' then state_ab='AZ' ;    if cntr_state='04' then state_ab='AR' ;
    if cntr_state='05' then state_ab='CA' ;    if cntr_state='06' then state_ab='CO' ;
    if cntr_state='07' then state_ab='CT' ;    if cntr_state='08' then state_ab='DE' ;
    if cntr_state='09' then state_ab='DC' ;    if cntr_state='10' then state_ab='FL' ;

    if cntr_state='11' then state_ab='GA' ;    if cntr_state='12' then state_ab='HI' ;
    if cntr_state='13' then state_ab='ID' ;    if cntr_state='14' then state_ab='IL' ;
    if cntr_state='15' then state_ab='IN' ;    if cntr_state='16' then state_ab='IA' ;
    if cntr_state='17' then state_ab='KS' ;    if cntr_state='18' then state_ab='KY' ;
    if cntr_state='19' then state_ab='LA' ;    if cntr_state='20' then state_ab='ME' ;

    if cntr_state='21' then state_ab='MD' ;    if cntr_state='22' then state_ab='MA' ;
    if cntr_state='23' then state_ab='MI' ;    if cntr_state='24' then state_ab='MN' ;
    if cntr_state='25' then state_ab='MS' ;    if cntr_state='26' then state_ab='MO' ;
    if cntr_state='27' then state_ab='MT' ;    if cntr_state='28' then state_ab='NE' ;
    if cntr_state='29' then state_ab='NV' ;    if cntr_state='30' then state_ab='NH' ;

    if cntr_state='31' then state_ab='NJ' ;    if cntr_state='32' then state_ab='NM' ;
    if cntr_state='33' then state_ab='NY' ;    if cntr_state='34' then state_ab='NC' ;
    if cntr_state='35' then state_ab='ND' ;    if cntr_state='36' then state_ab='OH' ;
    if cntr_state='37' then state_ab='OK' ;    if cntr_state='38' then state_ab='OR' ;
    if cntr_state='39' then state_ab='PA' ;    

    if cntr_state='41' then state_ab='RI' ;    if cntr_state='42' then state_ab='SC' ;
    if cntr_state='43' then state_ab='SD' ;    if cntr_state='44' then state_ab='TN' ;
    if cntr_state='45' then state_ab='TX' ;    if cntr_state='46' then state_ab='UT' ;
    if cntr_state='47' then state_ab='VT' ;
    if cntr_state='49' then state_ab='VA' ;    if cntr_state='50' then state_ab='WA' ;

    if cntr_state='51' then state_ab='WV' ;    if cntr_state='52' then state_ab='WI' ;
    if cntr_state='53' then state_ab='WY' ;

    length region9 region4 $ 25;

    if state_ab in ('CT','MA','ME','NH','RI','VT')                then cntr_region9='1.New England';
    if state_ab in ('NJ','NY','PA')                               then cntr_region9='2.Mid Atlantic';
    if state_ab in ('WI','MI','IL','IN','OH')                     then cntr_region9='3.East North Central';
    if state_ab in ('ND','SD','NE','KS','MN','IA','MO')           then cntr_region9='4.West North Central';
    if state_ab in ('DE','MD','DC','FL','GA','NC','SC','VA','WV') then cntr_region9='5.South Atlantic';
    if state_ab in ('KY','TN','MS','AL')                          then cntr_region9='6.East South Central';
    if state_ab in ('OK','AR','TX','LA')                          then cntr_region9='7.West South Central';
    if state_ab in ('MT','ID','WY','NV','UT','CO','AZ','NM')      then cntr_region9='8.Mountain';
    if state_ab in ('WA','OR','CA','HI','AK')                     then cntr_region9='9.Pacific';

    if cntr_region9='1.New England'        or cntr_region9='2.Mid Atlantic'        then cntr_region4='Region 1: Northeast';
    if cntr_region9='3.East North Central' or cntr_region9='4.West North Central'  then cntr_region4='Region 2: Midwest';
    if cntr_region9='5.South Atlantic'     or cntr_region9='6.East South Central'
      or region9='7.West South Central'                                            then cntr_region4='Region 3: South';
    if cntr_region9='8.Mountain'           or cntr_region9='9.Pacific'             then cntr_region4='Region 4: West';

       proc freq;
            tables cntr_region9;
            title 'Contract Level distribution';

       proc means n nmiss mean min max maxdec=2;
            class onest50pct;
            var pct_st;

  
  **-- Output Contract Level Measures --**;


  data a; set a0;

     %let measures = fspcabgn  fspptcan  fsp_cc_n  fsp_ce_n  fsp_rffn  fsp_thrn fsp_tkrn
                     fsp_xlin  fsp_oc_n  fsp_clcn  fsp_ah_n  fsp_vh_n  fsp_p_n  fsp_masn  fsp_lumn 
                     acd_n_t   acd_s_n   acd_m_n   acdy_n_t  acdy_s_n  acdy_m_n 
                     ac_ov_n   ac_erv_n  ac_asp_n  ac_ors_n ;


       %macro test(grp,outmon,outdata,outplan);

             proc means data=a sum maxdec=0 nway noprint;
                  class &grp;
                  var m_months;
                  output out=&outmon sum=totmonth;

             proc means data=a n std sum maxdec=2 nway noprint;
                  class &grp;
                  var &measures ;
                  output out=&outdata n= std= sum= /autoname;

            data &outplan; merge &outmon &outdata; by &grp;

                 ***********************************************************************;
                 **  Note: Compute Contract Level Utilization Measures as             **; 
                 **        Num procedures/discharges/days/visits per 1000 member year **;
                 ***********************************************************************;

                 n_cabg = (fspcabgn_sum/totmonth)*1000*12;  label n_cabg='CABG';
                 n_ptca = (fspptcan_sum/totmonth)*1000*12;  label n_ptca='PTCA';
                 n_cc   = (fsp_cc_n_sum/totmonth)*1000*12;  label n_cc  ='CardiacCathetherization';
                 n_ce   = (fsp_ce_n_sum/totmonth)*1000*12;  label n_ce  ='CardiacEnarterectomy';
                 n_rff  = (fsp_rffn_sum/totmonth)*1000*12;  label n_rff ='ReductionFemurFracture';
                 n_thr  = (fsp_thrn_sum/totmonth)*1000*12;  label n_thr ='TotalHipReplacement';
                 n_tkr  = (fsp_tkrn_sum/totmonth)*1000*12;  label n_tkr ='TotalKneereplacement';
                 n_xli  = (fsp_xlin_sum/totmonth)*1000*12;  label n_xli ='ExcisionLargeIntest';
                 n_oc   = (fsp_oc_n_sum/totmonth)*1000*12;  label n_oc  ='OpenCholecystectomy';
                 n_clc  = (fsp_clcn_sum/totmonth)*1000*12;  label n_clc ='ClosedCholecstectomy';
                 n_ah   = (fsp_ah_n_sum/totmonth)*1000*12;  label n_ah  ='AbdominalHysterectomy';
                 n_vh   = (fsp_vh_n_sum/totmonth)*1000*12;  label n_vh  ='VaginalHysterectomy';
                 n_p    = (fsp_p_n_sum/totmonth)*1000*12;   label n_p   ='Prostatectomy';
                 n_mast = (fsp_masn_sum/totmonth)*1000*12;  label n_mast='Masterctomy' ;
                 n_lump = (fsp_lumn_sum/totmonth)*1000*12;  label n_lump='Lumpectomy' ;
 
                iptotdis   = (acd_n_t_sum/totmonth)*1000*12;  label iptotdis ='InpatTotalDischarges';
                ipsurdis   = (acd_s_n_sum/totmonth)*1000*12;  label ipsurdis ='InpatSurgeryDischarges';
                ipmeddis   = (acd_m_n_sum/totmonth)*1000*12;  label ipmeddis ='InpatMedicineDischarges';

                iptotdays  = (acdy_n_t_sum/totmonth)*1000*12; label iptotdays ='InpatTotalDays';
                ipsurdays  = (acdy_s_n_sum/totmonth)*1000*12; label ipsurdays ='InpatSurgeryDays';
                ipmeddays  = (acdy_m_n_sum/totmonth)*1000*12; label ipmeddays ='InpatMedicineDays';

                amb_opv = (ac_ov_n_sum/totmonth)*1000*12;   label amb_opv ='OutPatientVisits';
                amb_erv = (ac_erv_n_sum/totmonth)*1000*12;  label amb_erv ='EmergencyRoomVisits';
                amb_asp = (ac_asp_n_sum/totmonth)*1000*12;  label amb_asp ='AbmSugeryProcedures';
                amb_ors = (ac_ors_n_sum/totmonth)*1000*12 ; label amb_ors ='ObservationRoomStays';

       %mend;

       %test(org_id,    org_mon,       org_dat,       org_util);

       %macro rates(grp,outdat);

             proc means data=a(where=(bcs_d=1)) mean nway noprint;
                  class &grp;
                  var bcs_n; 
                  output out=planbcs mean=bcs;
 
             proc means data=a(where=(cmc_d=1)) mean nway noprint;
                  class &grp;
                  var cmc_n1;
                  output out=plancmc mean=cmc;

             proc means data=a(where=(cdc_d1=1)) mean nway noprint;
                  class &grp;
                  var cdc_n1;
                  output out=plancdc1 mean=cdc1;

             proc means data=a(where=(cdc_d3=1)) mean nway noprint;
                  class &grp;
                  var cdc_n3;
                  output out=plancdc3 mean=cdc3;

             proc means data=a(where=(cdc_d4=1)) mean nway noprint;
                  class &grp;
                  var cdc_n4;
                  output out=plancdc4 mean=cdc4;

            proc means data=a(where=(gso_d=1)) mean nway noprint;
                  class &grp;
                  var gso_n;
                  output out=plangso mean=gso;

       
          data &outdat; merge planbcs plancmc plancdc1 plancdc3 plancdc4 plangso; by &grp;

                 format bcs cmc cdc1 cdc3 cdc4 f8.4;

                 label bcs  ='Breast Cancer Screening 65-69';
                 label cmc  ='LDL-C Screening After AccuteCare';
                 label cdc1 ='Diabetes: HbA1cTesting';
                 label cdc3 ='Diabetes: EyeExam';
                 label cdc4 ='Diabetes: LCL-C Screening';
                 label gso  ='Glaucoma Screening';

            proc sort; by &grp;


    %mend;
 
    %rates(org_id,     org_rate);

   data orgid; merge org_util org_rate; by org_id;

        proc sort; by org_id;

   data plan.cntr2005(drop= _freq_ _type_ ); merge org orgid; by org_id;
       
       proc contents data=plan.cntr2005 varnum;

endsas;




